﻿#region 命名空间

using System;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.FriendLink
{
    /// <summary>
    ///     友情链接管理
    /// </summary>
    [Access("259001")]
    public partial class FriendLink : WebFactory.PageBase
    {
        private readonly FriendLinkManage _myBLL = new FriendLinkManage();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var action = Request["Action"];
                if (action == "Edit")
                {
                    this.GV_List.Visible = false;
                    this.Panel1.Visible = true;

                    var model = this._myBLL.GetInfo(Request["ID"]);

                    this.txtTitle.Text = model.Title;
                    this.txtLogo.Text = model.Logo;
                    this.Img_Logo.ImageUrl = model.Logo;
                    this.txtURL.Text = model.Url;
                    this.cbkPass.Checked = model.Pass;
                }
                else if (action == "Add")
                {
                    this.GV_List.Visible = false;
                    this.Panel1.Visible = true;
                }
                else
                {
                    this.GV_List.Visible = true;
                    this.Panel1.Visible = false;

                    this.GV_List.EmptyDataTemplate = GetTemplate(this);

                    this._myBLL.BindList(this.GV_List);
                }
            }
        }


        protected void btnOK_Click(object sender, EventArgs e)
        {
            var model = new WinnerCMS.Model.FriendLink();
            if (Request["ID"] == null)
            {
                model.Id = 0;
            }
            else
            {
                model.Id = int.Parse(Request["ID"]);
            }
            model.Title = this.txtTitle.Text;
            model.Logo = this.txtLogo.Text;
            model.Url = this.txtURL.Text;
            model.Pass = this.cbkPass.Checked;

            this._myBLL.Add(model, this.UpdatePanel1);
        }


        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
            {
                return;
            }

            var myTableCell = e.Row.Cells[8];

            var strTemp = myTableCell.Text.Trim();
            if (MyValidate.IsNumeric(strTemp))
            {
                int I = Convert.ToInt16(strTemp);
                if (I == 1)
                {
                    myTableCell.Text = "已审核";
                }
                else
                {
                    myTableCell.Text = "未审核";
                }
            }
            else
            {
                var b = Convert.ToBoolean(strTemp);
                if (b)
                {
                    myTableCell.Text = "已审核";
                }
                else
                {
                    myTableCell.Text = "未审核";
                }
            }
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var ID = this.GV_List.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();

            if (e.CommandName.ToLower() == "up")
            {
                this._myBLL.UpDown(ID, "up");
            }
            if (e.CommandName.ToLower() == "down")
            {
                this._myBLL.UpDown(ID, "down");
            }

            this._myBLL.BindList(this.GV_List);
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var ID = this.GV_List.DataKeys[e.RowIndex].ToString();

            this._myBLL.Delete(ID, this.UpdatePanel1);

            this._myBLL.BindList(this.GV_List);
        }
    }
}